package com.wr.spinkLock14;


import java.util.concurrent.atomic.AtomicReference;

// 简易自旋锁
// 原子CAS操作
public class SpinLock01 {

    private AtomicReference<Thread> sign = new AtomicReference<>();  // 原子类

    public void lock(){
        Thread thread = Thread.currentThread();
        while (!sign.compareAndSet(null,thread)){}
    }

    public void unlock(){
        Thread thread = Thread.currentThread();
        sign.compareAndSet(thread,null);
    }

}
